﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CERT.Services.DAL;
using System.Configuration;
using RefactorThis.GraphDiff;


namespace CERT.Services
{
    public partial class NETServices : CERT.Services.INETServices
    {
        public IEnumerable<Team> GetAllTeams()
        {
            using (var context = new NETEntities())
            {
                return context.Teams.ToList<Team>().AsEnumerable<Team>(); 
            
        }
           
        }

        public Team GetSingleTeam(System.Linq.Expressions.Expression<Func<Team, bool>> predicate)
        {

           
            using (var context = new NETEntities())
            {

                var entry = context.Set<Team>().Include("People_TeamMember").Include("People_TeamMember.Person").Include("Incidents").FirstOrDefault<Team>(predicate);
                return entry;
            }
        }

        public void DeleteTeam(int id)
        {

            //todo ideally, you check and see if this the user's only association.
            //if so, delete them.
            using (var context = new NETEntities())
            {
                var entity = context.Set<Team>().FirstOrDefault(a => a.Id == id);
                context.Set<Team>().Remove(entity);
                context.SaveChanges();
            }

        }

        public void DeleteTeamMember(int id)
        {

            //todo ideally, you check and see if this the user's only association.
            //if so, delete them.
            using (var context = new NETEntities())
            {
                var entity = context.Set<People_TeamMember>().FirstOrDefault(a => a.Id == id);
                context.Set<People_TeamMember>().Remove(entity);
                context.SaveChanges();
            }

        }

        public void AddTeamMember(People_TeamMember entity)
        {
            //replace with...
            //http://refactorthis.wordpress.com/2012/12/11/introducing-graphdiff-for-entity-framework-code-first-allowing-automated-updates-of-a-graph-of-detached-entities/#comments

            using (var context = new NETEntities())
            {
               
                var existing = context.Teams.Find(entity.TeamId);
                existing.People_TeamMember.Add(entity);
                context.SaveChanges();

            }

        }

        public void UpdateTeamMember(People_TeamMember entity, int teamMemberId)
        {
            //replace with...
            //http://refactorthis.wordpress.com/2012/12/11/introducing-graphdiff-for-entity-framework-code-first-allowing-automated-updates-of-a-graph-of-detached-entities/#comments

            using (var context = new NETEntities())
            {

                context.UpdateGraph<People_TeamMember>(entity);

                var existing = context.Teams.Find(entity.TeamId);
                context.Entry(existing.People_TeamMember.FirstOrDefault(a => a.Id == teamMemberId)).CurrentValues.SetValues(entity);
                context.SaveChanges();

              }

        }

        public void AddTeam(Team entity)
        {
            using (var context = new NETEntities())
            {
               
                context.Teams.Add(entity);
                context.SaveChanges();
            }

        }
    }
}